package com.wmlive.hhvideo.heihei.recordvideo;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;

import com.dyuiapi.api.DyUIAPI;
import com.dyuiapi.api.model.MusicInfo;
import com.wmlive.hhvideo.application.DCApplication;
import com.wmlive.hhvideo.common.base.BaseCompatActivity;
import com.wmlive.hhvideo.heihei.recordvideo.utils.Utils;
import com.wmlive.hhvideo.utils.ToastUtil;

import java.util.Arrays;
import java.util.List;

import cn.wmlive.hhvideo.R;

import static android.app.Activity.RESULT_OK;

/**
 * 第三方视频录制的管理器
 * Created by kangzhen on 2017/6/8.
 */

public class DyUIAPIManager {
    private static DyUIAPIManager dyUIAPIManager;
    public final static int REQUEST_VIDEO_CODE = 130;//打开本地视频
    public UserShorVideoInfoExtraInfo mUserShorVideoInfoExtraInfo;//打开本地视频的扩展信息

    //第三方权限
    String[] permissionParams = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA};


    private DyUIAPIManager() {

    }

    public static DyUIAPIManager getInstance() {
        if (dyUIAPIManager == null) {
            synchronized (DyUIAPIManager.class) {
                if (dyUIAPIManager == null) {
                    dyUIAPIManager = new DyUIAPIManager();
                }
            }
        }
        return dyUIAPIManager;
    }

    /**
     * 开拍
     *
     * @param context
     */
    public void onCamera(final Context context, final boolean isFinish) {
        if (context == null) {
            return;
        }
        if (context instanceof Activity) {
            ((BaseCompatActivity) context).checkPermissionsStatus(permissionParams, new BaseCompatActivity.PermissionCallbackInterface() {
                @Override
                public void onPermissionsHasCallback() {
                    //已经包含了权限
                    if (!DyUIAPI.isInitialized()) {
                        DCApplication appImp = (DCApplication) DCApplication.getDCApp();
                        appImp.initializeDyUIAPI();
                    }
                    DyUIAPI.onCamera(context, null, null);
                    if (isFinish) {
                        ((Activity) context).finish();
                    }
                }

                @Override
                public void onPermissionsGrantedCallback(int requestCode, List<String> params) {
                    if (permissionParams != null && permissionParams.length > 0) {
                        List<String> permissionList = Arrays.asList(permissionParams);
                        if (params.containsAll(permissionList) && permissionList.containsAll(params)) {
                            //全部授权
                            if (!DyUIAPI.isInitialized()) {
                                DCApplication appImp = (DCApplication) DCApplication.getDCApp();
                                appImp.initializeDyUIAPI();
                            }
                            DyUIAPI.onCamera(context, null, null);
                            if (isFinish) {
                                ((Activity) context).finish();
                            }
                        }
                    }
                }

                @Override
                public void onPermissionsDeniedCallback(int requestCode, List<String> params) {
                    ToastUtil.showToast(R.string.permission_error_msg);
                }
            });
        }

    }

    /**
     * 带着音乐开拍
     *
     * @param context
     * @param musicInfo
     */
    public void onCameraByMusicInfo(final Context context, final MusicInfo musicInfo, final boolean isFinish) {
        if (context == null) {
            return;
        }
        if (context instanceof Activity) {
            ((BaseCompatActivity) context).checkPermissionsStatus(permissionParams, new BaseCompatActivity.PermissionCallbackInterface() {
                @Override
                public void onPermissionsHasCallback() {
                    //已经包含了权限
                    DyUIAPI.onCamera(context, musicInfo, null);
                    if (isFinish) {
                        ((Activity) context).finish();
                    }
                }

                @Override
                public void onPermissionsGrantedCallback(int requestCode, List<String> params) {
                    if (permissionParams != null && permissionParams.length > 0) {
                        List<String> permissionList = Arrays.asList(permissionParams);
                        if (params.containsAll(permissionList) && permissionList.containsAll(params)) {
                            //全部授权
                            if (!DyUIAPI.isInitialized()) {
                                DCApplication appImp = (DCApplication) DCApplication.getDCApp();
                                appImp.initializeDyUIAPI();
                            }
                            DyUIAPI.onCamera(context, musicInfo, null);
                            if (isFinish) {
                                ((Activity) context).finish();
                            }
                        }
                    }
                }

                @Override
                public void onPermissionsDeniedCallback(int requestCode, List<String> params) {
                    ToastUtil.showToast(R.string.permission_error_msg);
                }
            });
        }

    }


    /**
     * 带着话题扩展信息开拍
     *
     * @param context
     * @param userShorVideoInfoExtraInfo
     */
    public void onCameraByExtralInfo(final Context context, final UserShorVideoInfoExtraInfo userShorVideoInfoExtraInfo, final boolean isFinish) {
        if (context == null) {
            return;
        }
        if (context instanceof Activity) {
            ((BaseCompatActivity) context).checkPermissionsStatus(permissionParams, new BaseCompatActivity.PermissionCallbackInterface() {
                @Override
                public void onPermissionsHasCallback() {
                    //已经包含了权限
                    DyUIAPI.onCamera(context, null, userShorVideoInfoExtraInfo);
                    if (isFinish) {
                        ((Activity) context).finish();
                    }
                }

                @Override
                public void onPermissionsGrantedCallback(int requestCode, List<String> params) {
                    if (permissionParams != null && permissionParams.length > 0) {
                        List<String> permissionList = Arrays.asList(permissionParams);
                        if (params.containsAll(permissionList) && permissionList.containsAll(params)) {
                            //全部授权
                            if (!DyUIAPI.isInitialized()) {
                                DCApplication appImp = (DCApplication) DCApplication.getDCApp();
                                appImp.initializeDyUIAPI();
                            }
                            DyUIAPI.onCamera(context, null, userShorVideoInfoExtraInfo);
                            if (isFinish) {
                                ((Activity) context).finish();
                            }
                        }
                    }
                }

                @Override
                public void onPermissionsDeniedCallback(int requestCode, List<String> params) {
                    ToastUtil.showToast(R.string.permission_error_msg);
                }
            });
        }

    }

    /**
     * 带着音乐和扩展信息开拍
     *
     * @param context
     * @param userShorVideoInfoExtraInfo
     */
    public void onCameraByMusicInfoAndExtralInfo(final Context context, final MusicInfo musicInfo, final UserShorVideoInfoExtraInfo userShorVideoInfoExtraInfo, final boolean isFinish) {
        if (context == null) {
            return;
        }
        if (context instanceof Activity) {
            ((BaseCompatActivity) context).checkPermissionsStatus(permissionParams, new BaseCompatActivity.PermissionCallbackInterface() {
                @Override
                public void onPermissionsHasCallback() {
                    //已经包含了权限
                    if (!DyUIAPI.isInitialized()) {
                        DCApplication appImp = (DCApplication) DCApplication.getDCApp();
                        appImp.initializeDyUIAPI();
                    }
                    DyUIAPI.onCamera(context, musicInfo, userShorVideoInfoExtraInfo);
                    if (isFinish) {
                        ((Activity) context).finish();
                    }
                }

                @Override
                public void onPermissionsGrantedCallback(int requestCode, List<String> params) {
                    if (permissionParams != null && permissionParams.length > 0) {
                        List<String> permissionList = Arrays.asList(permissionParams);
                        if (params.containsAll(permissionList) && permissionList.containsAll(params)) {
                            //全部授权
                            if (!DyUIAPI.isInitialized()) {
                                DCApplication appImp = (DCApplication) DCApplication.getDCApp();
                                appImp.initializeDyUIAPI();
                            }
                            DyUIAPI.onCamera(context, musicInfo, userShorVideoInfoExtraInfo);
                            if (isFinish) {
                                ((Activity) context).finish();
                            }
                        }
                    }
                }

                @Override
                public void onPermissionsDeniedCallback(int requestCode, List<String> params) {
                    ToastUtil.showToast(R.string.permission_error_msg);
                }
            });
        }


    }

    /**
     * 打开本地视频
     */
    public void onOpenLocalVideoInfos(Context context, UserShorVideoInfoExtraInfo userShorVideoInfoExtraInfo) {
        if (context == null) {
            return;
        }
        if (context instanceof Activity) {
            mUserShorVideoInfoExtraInfo = userShorVideoInfoExtraInfo;
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("video/*");
            Intent wrapperIntent = Intent.createChooser(intent, null);
            ((Activity) context).startActivityForResult(wrapperIntent, REQUEST_VIDEO_CODE);
        }
    }

    /**
     * 处理打开本地视频的回调参数
     *
     * @param context
     * @param requestCode
     * @param resultCode
     * @param data
     */
    public void onActivityResultByVideoInfos(Context context, int requestCode, int resultCode, Intent data) {
        if (context == null) {
            return;
        }
        if (REQUEST_VIDEO_CODE == requestCode && context instanceof Activity) {
            if (resultCode == RESULT_OK) {
                Uri uri = data.getData();
                String str = Utils.getAbsolutePath((Activity) context, uri);
                DyUIAPI.directUpload(context, str, mUserShorVideoInfoExtraInfo);
            }
        }
    }

    /**
     * 本地视频
     *
     * @param context
     * @param path
     */
    public void onOpenLocalVideoInfos(Context context, String path) {
        if (context == null) {
            return;
        }
        DyUIAPI.directUpload(context, path, mUserShorVideoInfoExtraInfo);
    }


    /**
     * 检查未编辑完成的视频信息
     *
     * @param context
     */
    public void onCheckUnFinishData(final Context context) {
        if (context == null) {
            return;
        }
        if (DyUIAPI.existsEditingShortVideo(context)) {
            new AlertDialog.Builder(context)
                    .setMessage(R.string.exists_editing_video)
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            DyUIAPI.continueShortVideoEdit(context, null);
                        }
                    })
                    .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            DyUIAPI.clearEditingShortVideo(context);
                        }
                    })
                    .show();
        }
    }
}
